/*
Mif.Menu.Load
*/
Mif.Menu.Load={
		
	list: function(json, structure){
		var options=json.options;
		var items=json.items;
		var list=new Mif.Menu.List(structure, options);
		Mif.Menu.Load.items(items, {owner:list});
		list.owner.fireEvent('load', list);
		return list;
	},
	
	items: function(items, structure){
		for( var i=items.length; i--; ){
			var item=items[i];
			var list=item.list;
			delete item.list;
			var item=new Mif.Menu.Item(structure, item);
			structure.owner.items.unshift(item);
			if(list){
				var list=Mif.Menu.Load.list(list, {owner:structure.owner.owner, ancestor: item});
				item.subMenu=list;
				item.loaded=true;
			}
		}
	}
	
};

Mif.Menu.implement({

	load: function(options){
		var menu=this;
		this.loadOptions=this.loadOptions||$lambda({});
		function success(json){
			var list=Mif.Menu.Load.list(json, {ancestor:null,owner:menu});
			menu.child=list;
			menu.fireEvent('load');
		}
		options=$extend($extend({
			isSuccess: $lambda(true),
			secure: true,
			onSuccess: success,
			method: 'get'
		}, this.loadOptions()), options);
		if(options.json) {
			success(options.json);
		}else{
			new Request.JSON(options).send();
		}
		return this;
	}
	
});

Mif.Menu.Item.implement({
	
	load: function(options){
		this.$loading=true;
		this.setLoader();
		var item=this;
		function success(json){
			var list=Mif.Menu.Load.list(json, {ancestor: item, owner: item.owner.owner});
			item.child=list;
			delete item.$loading;
			item.state.loaded=true;
			item.removeLoader();
			item.fireEvent('load');
			item.owner.owner.fireEvent('load', list);
		}
		options=$extend($extend($extend({
			isSuccess: $lambda(true),
			secure: true,
			onSuccess: success,
			method: 'get'
		}, this.menu.loadOptions(this)), this.loadOptions), options);
		if(options.json) {
			success(options.json);
		}else{
			new Request.JSON(options).send();
		}
		return this;
	},
	
	setLoader: function(){
	},
	
	removeLoader: function(){
	}
	
});